package com.jingfei.drools.controller;

import com.jingfei.drools.cls.Person;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author jingfei
 * @Date 2022/7/25 23:16
 */

@Slf4j
@RestController
public class DroolsController {

    @GetMapping("test")
    public void test() {
        KieServices kieService = KieServices.Factory.get();
        KieContainer kieContainer = kieService.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession("testKieSession");
        int count = kieSession.fireAllRules();
        log.info("总共执行了" + count + "条规则！");
        kieSession.dispose();
    }

    @GetMapping("test2")
    public void test2() {
        KieServices kieService = KieServices.Factory.get();
        KieContainer kieContainer = kieService.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession("testKieSession");
        Person person = new Person("荆飞", 25);
        kieSession.insert(person);
        int count = kieSession.fireAllRules();
        log.info("总共执行了" + count + "条规则！");
        kieSession.dispose();
    }

}
